{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"RequestId\":\"bc7f5b60-c57a-4a8f-a69d-8f01ded6523c\",\"errorCode\":\"3008\",\"l\":\"zh-CHS2en\"}'\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import sys\n",
    "import uuid\n",
    "import requests\n",
    "import wave\n",
    "import base64\n",
    "import hashlib\n",
    "\n",
    "from imp import reload\n",
    "\n",
    "\n",
    "reload(sys)\n",
    "\n",
    "YOUDAO_URL = 'https://openapi.youdao.com/speechtransapi'\n",
    "APP_KEY = '4ae8cc4d4e76466a'\n",
    "APP_SECRET = 'OA2Xb4UKOZmkHSGnd71S7tPr2J78FEtA'\n",
    "\n",
    "def truncate(q):\n",
    "    if q is None:\n",
    "        return None\n",
    "    size = len(q)\n",
    "    return q if size <= 20 else q[0:10] + str(size) + q[size-10:size]\n",
    "\n",
    "def encrypt(signStr):\n",
    "    hash_algorithm = hashlib.md5()\n",
    "    hash_algorithm.update(signStr.encode('utf-8'))\n",
    "    return hash_algorithm.hexdigest()\n",
    "\n",
    "def do_request(data):\n",
    "    headers = {'Content-Type': 'application/x-www-form-urlencoded'}\n",
    "    return requests.post(YOUDAO_URL, data=data, headers=headers)\n",
    "\n",
    "def connect():\n",
    "    audio_file_path = 'C:/Users/86134/Desktop/translate5.wav'\n",
    "    lang_type = '合成文本的语言类型'\n",
    "    extension = audio_file_path[audio_file_path.rindex('.')+1:]\n",
    "    if extension != 'wav':\n",
    "        print('不支持的音频类型')\n",
    "        sys.exit(1)\n",
    "    wav_info = wave.open(audio_file_path, 'rb')\n",
    "    sample_rate = wav_info.getframerate()\n",
    "    nchannels = wav_info.getnchannels()\n",
    "    wav_info.close()\n",
    "    with open(audio_file_path, 'rb') as file_wav:\n",
    "        q = base64.b64encode(file_wav.read()).decode('utf-8')\n",
    "\n",
    "    data = {}\n",
    "    data['from'] = 'zh-CHS'\n",
    "    data['to'] = 'en'\n",
    "    salt = str(uuid.uuid1())\n",
    "    signStr = APP_KEY + q + salt + APP_SECRET\n",
    "    sign = encrypt(signStr)\n",
    "    data['appKey'] = APP_KEY\n",
    "    data['q'] = q\n",
    "    data['salt'] = salt\n",
    "    data['sign'] = sign\n",
    "    data['signType'] = \"v1\"\n",
    "    data['langType'] = lang_type\n",
    "    data['rate'] = sample_rate\n",
    "    data['format'] = 'wav'\n",
    "    data['channel'] = nchannels\n",
    "    data['type'] = 1\n",
    "\n",
    "    response = do_request(data)\n",
    "    print(response.content)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    connect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
